package leetcode.editor.cn;

public class _4_MedianOfTwoSortedArrays {
    public static void main(String[] args) {
        Solution solution = new _4_MedianOfTwoSortedArrays().new Solution();
        int[] nums1 = {1, 3};
        int[] nums2 = {2};
        System.out.println(solution.findMedianSortedArrays(nums1, nums2));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            double res = 0;
            int n1 = nums1.length, n2 = nums2.length;
            int[] nums = new int[n1 + n2];
            int idx1 = 0, idx2 = 0, idx = 0;
            while (idx1 < n1 && idx2 < n2) {
                if (nums1[idx1] < nums2[idx2]) {
                    nums[idx++] = nums1[idx1++];
                } else {
                    nums[idx++] = nums2[idx2++];
                }
            }
            while (idx1 < n1) {
                nums[idx++] = nums1[idx1++];
            }
            while (idx2 < n2) {
                nums[idx++] = nums2[idx2++];
            }
            res = (nums[(n1 + n2) / 2] + nums[(n1 + n2 - 1) / 2]) / 2.0;
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}